Skip to content

Make RowsAffected faster#2455

Merged
jackc merged 1 commit into
jackc:masterfrom
achanda:faster-rowsaffected
Dec 27, 2025
Merged

Make RowsAffected faster#2455
jackc merged 1 commit into
jackc:masterfrom
achanda:faster-rowsaffected

Conversation

@achanda

@achanda achanda commented Dec 27, 2025

Copy link
Copy Markdown
Contributor

The current implementation uses two loops, first one to find out where digits start and second one to loop over the digits. The modified implementation starts at the end and loops till digits end. It is a bit faster than baseline.

$ go test -run=NONE -bench=BenchmarkCommandTagRowsAffected -benchmem -count=10 > /tmp/new.txt
$ git checkout master
$ go test -run=NONE -bench=BenchmarkCommandTagRowsAffected -benchmem -count=10 > /tmp/old.txt
$ benchstat /tmp/old.txt /tmp/new.txt
goos: darwin
goarch: arm64
pkg: github.com/jackc/pgx/v5/pgconn
cpu: Apple M1 Max
                                             │ /tmp/old.txt │            /tmp/new.txt             │
                                             │    sec/op    │   sec/op     vs base                │
CommandTagRowsAffected/UPDATE_1-10              3.166n ± 3%   2.075n ± 1%  -34.44% (p=0.000 n=10)
CommandTagRowsAffected/UPDATE_123456789-10     10.045n ± 2%   6.237n ± 2%  -37.90% (p=0.000 n=10)
CommandTagRowsAffected/INSERT_0_1-10            3.211n ± 1%   2.085n ± 1%  -35.08% (p=0.000 n=10)
CommandTagRowsAffected/INSERT_0_123456789-10   10.120n ± 3%   6.327n ± 7%  -37.48% (p=0.000 n=10)
geomean                                         5.670n        3.615n       -36.25%

                                             │ /tmp/old.txt │            /tmp/new.txt             │
                                             │     B/op     │    B/op     vs base                 │
CommandTagRowsAffected/UPDATE_1-10             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/UPDATE_123456789-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/INSERT_0_1-10           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/INSERT_0_123456789-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                   ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                             │ /tmp/old.txt │            /tmp/new.txt             │
                                             │  allocs/op   │ allocs/op   vs base                 │
CommandTagRowsAffected/UPDATE_1-10             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/UPDATE_123456789-10     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/INSERT_0_1-10           0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CommandTagRowsAffected/INSERT_0_123456789-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                   ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

The current implementation uses two loops, first one to find out
where digits start and second one to loop over the digits. The
modified implementation starts at the end and loops till digits
end. It is a bit faster than baseline.
@acelinkio

Copy link
Copy Markdown

Reading through pgx docs and came across this while exploring.

Curious. Could those performance gains be from caching on the postgres side? Or does the testsuite have a way to reset?

@jackc

jackc commented Dec 27, 2025

Copy link
Copy Markdown
Owner

@acelinkio The benchmark is only testing parsing a command tag. There is no PostgreSQL server in those tests.

@jackc jackc merged commit ae17216 into jackc:master Dec 27, 2025
3 of 14 checks passed
@achanda achanda deleted the faster-rowsaffected branch December 27, 2025 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants